import $ from '../common';
Page({
  data: {
    titleHeight: $.gData().titleHeight,
    statuHeight: $.gData().statuHeight,
    isIphoneX: $.gData().isIphoneX,
  },

  afterRead(event) { //上传图片前的处理
    const {
      imgs,
      field
    } = this.data
    wx.chooseImage({
      count: 9,
      success: (res) => {
        let obj = {
          fileList: res.tempFilePaths,
          prefix: field
        }
        $.uploadFileList('/image/upload/oss', obj, ras => {
          let arr
          if (imgs) {
            arr = [...imgs, ...ras]
          } else {
            arr = [...ras]
          }
          this.upimg(arr)
        })
      }
    })
  },
  upimg(arr) {//上传图片
    const {field}=this.data
    let data = {
      teacher_uuid: wx.getStorageSync('teacher_uuid'),
      campus_uuid: wx.getStorageSync('campus_uuid'),
    }
    data[field] = arr
    $.post("/teacher/update", data, (res) => {
      this.setData({
        imgs: arr
      })
    })
  },
  magnifyimg(e) { //点击图片放大
    const {
      img: src
    } = e.currentTarget.dataset
    const {
      imgs
    } = this.data
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgs // 需要预览的图片http链接列表
    })
  },
  imgdelete(event) { //删除图片
    const { imgs} = this.data
    imgs.splice(event.currentTarget.dataset.index, 1)
    this.upimg(imgs)
  },
  onLoad(e) {
    const {tiile,field,imgs} = e
    this.setData({
      tiile,
      field,
      imgs: JSON.parse(imgs)
    })
  },
  onShow() {}
})